package com.um.psystem.service.mtService.impl;

import com.um.psystem.mapper.platform.PublicMapper;
import com.um.psystem.service.mtService.IAssetsApplyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: AiLen2E
 * @Date: 2021/11/26 8:15
 * @version: 1.0
 */
@Service
public class AssetsApplyRecordService implements IAssetsApplyRecordService {

    @Autowired
    PublicMapper publicMapper;

    @Override
    public List<Map<String, Object>> getAssetsApplyRecord(Map map) {
        String sqltmp = " WHERE 1=1";

        Map paramsMap = new HashMap();
        Integer page = Integer.parseInt((map.get("page") != null ? map.get("page").toString() : "0"));
        Integer rows = Integer.parseInt((map.get("rows") != null ? map.get("rows").toString() : "0"));

        if (map.containsKey("type_dtl_no") && (!map.get("type_dtl_no").toString().equals(""))) {
            String type_dtl_no = map.get("type_dtl_no").toString();
            paramsMap.put("type_dtl_no", type_dtl_no);
            sqltmp +=" AND type_dtl_no=#{type_dtl_no}";
        }

        if (map.containsKey("apply_id") && (!map.get("apply_id").toString().equals(""))) {
            String apply_id = map.get("apply_id").toString();
            paramsMap.put("apply_id", apply_id);
            sqltmp +=" AND apply_id=#{apply_id}";
        }

        if (map.containsKey("apply_order_info_id") && (!map.get("apply_order_info_id").toString().equals(""))) {
            String apply_order_info_id = map.get("apply_order_info_id").toString();
            paramsMap.put("apply_order_info_id", apply_order_info_id);
            sqltmp += " AND apply_order_info_id=#{apply_order_info_id}";
        }

        if (map.containsKey("apply_man") && (!map.get("apply_man").toString().equals(""))) {
            String apply_man = map.get("apply_man").toString();
            paramsMap.put("apply_man", apply_man);
            sqltmp += " AND apply_man=#{apply_man}";
        }

        if (map.containsKey("type_dtl_name") && (!map.get("type_dtl_name").toString().equals(""))) {
            String type_dtl_name = map.get("type_dtl_name").toString();
            paramsMap.put("type_dtl_name", type_dtl_name);
            sqltmp += " AND type_dtl_name LIKE CONCAT('%',#{type_dtl_name},'%')";
        }

        if (map.containsKey("type_dtl_spec") && (!map.get("type_dtl_spec").toString().equals(""))) {
            String type_dtl_spec = map.get("type_dtl_spec").toString();
            paramsMap.put("type_dtl_spec", type_dtl_spec);
            sqltmp += " AND sizes LIKE CONCAT('%',#{type_dtl_spec},'%')";
        }

        String sql = "SELECT COUNT(*) as totals " +
                "FROM ws_eng_assets_apply_info a " +
                "JOIN ws_eng_assets_type_dtl b ON a.assets_id=b.type_dtl_id"+sqltmp;

        paramsMap.put("sqlStr", sql);
        int totals = 0;
        List<Map<String, Object>> list_count = publicMapper.getPublicItems(paramsMap);
        if (list_count.size() > 0) {
            totals = Integer.parseInt(list_count.get(0).get("totals").toString());
        } else {
            System.out.println("****************:" + totals);
        }

        sql = "SELECT a.apply_id,a.apply_order_info_id,a.erp_no,a.apply_dept,a.apply_man,a.apply_num,a.remark,a.apply_time,a.state,a.refusereason,a.refusereason_user,a.refusereason_time,b.type_dtl_id,b.type_dtl_no,b.type_dtl_name,b.sizes,b.unit,"+totals+" as totals " +
                "FROM ws_eng_assets_apply_info a " +
                "JOIN ws_eng_assets_type_dtl b ON a.assets_id=b.type_dtl_id"+sqltmp;

        if (page != 0 && rows != 0) {
            sql += " limit #{startIndex},#{rows} ";
            paramsMap.put("startIndex", (page - 1) * rows);
            paramsMap.put("rows", rows);
        }
        paramsMap.put("sqlStr", sql);
        return publicMapper.getPublicItems(paramsMap);
    }
}
